﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Domain.Repositories;
using Core.Domain.Model;
using Core;
using SSO.Application1.heplpers;
using System.Net.Mail;
using System.Net;
namespace SSO.Application1.Controllers
{
	[HandleError]
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
            string test_encryption = "user='hai'&password='123'";
            string key = "hai";
            string ap = Encrypt.EncryptString(test_encryption, key);
           ViewData["test"] = ap;

            string de = Encrypt.DecryptString(ap, key);
            ViewData["app"] = de;
			

			return View();
		}

		public ActionResult About()
		{
			return View();
		}
        public ActionResult Registry() {


            
            return View();
        
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Registry(FormCollection form) 
        {


            string _mailServer = "smtp.gmail.com";
            int _mailPort = 587; // Work well, 465 Timeout

            string _user = "minhtruyet2@gmail.com";
            string _pass = "dth061123";

            string _mailFrom = "minhtruyet2@gmail.com";
            string _mailTo = form["mail"];
            string Username = form["name"];
            string Password = form["password"];
            string StringSend = "username={" + Username + "}&password={" + Password + "}&mail={" +_mailTo+"}";
            MailMessage mailMessage = new MailMessage();
            SmtpClient mailClient = new SmtpClient(_mailServer, _mailPort);
            mailClient.Timeout = 15000;
            mailClient.Credentials = new NetworkCredential(_user, _pass);
            mailClient.EnableSsl = true;
            string tam= Encrypt.EncryptString(StringSend,"hai");
            string connect_service = "http://localhost:21259/SSOService.svc/user/Registry?";
            string messageSend = connect_service + tam;

            try
            {

                mailMessage.IsBodyHtml = true;
                mailMessage.From = new MailAddress(_mailFrom);
                mailMessage.Subject = "Thank you registry!!";
                mailMessage.Body = "<html><body><b>You must config your accout!! check link</b>" + " <font color=\"red\">" + messageSend + "</font></body></html>";

                mailMessage.To.Add(_mailTo);
                mailClient.Send(mailMessage);

                Response.Write("Send mail success");
            }
            catch (Exception ex)
            {
                Response.Write("Send mail fail, " + ex.Message);
            }
            return View();

        
        }
	}
}
